package org.lex;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.lex.string.XmlUtils;

public class Test {
	public static void main(String[] args) {
		try {
			run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static void run() throws Exception {
		List<Element> list = new ArrayList<Element>();

		String dir = "C:\\_liuex\\tmp\\123";
		InputStream in = new FileInputStream(dir + "\\SQLDrivers.xml");
		Document doc = new SAXReader().read(in);
		in.close();

		Element root = doc.getRootElement();
		root.setName("Drivers");
		list.addAll(root.elements("Bean"));
		for (Element e : list) {
			e.setName("Driver");
			e.remove(e.attribute("Class"));
			e.remove(e.element("identifier"));
			e.remove(e.element("jarFileName"));
			e.remove(e.element("jarFileNames"));
			e.remove(e.element("usesClassPath"));

			Element url = e.element("url");
			url.setText(trans(url.getText()));
		}

		System.out.println(XmlUtils.format(doc));
	}

	static String trans(String url) {
		url = url.replace("<", "<${");
		url = url.replace(">", "}>");

		url = url.replace("[", "${");
		url = url.replace("]", "}");
		return url;
	}
}
